package edu.pku.sei.act.actools;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;
import edu.pku.sei.act.actools.util.EnvInfo;
import edu.pku.sei.act.actools.util.LocalTupleSpace;
import edu.pku.sei.act.actools.util.RemoteTupleSpace;

public class EnvEntry extends Service {
	static final String ltag = null;//"EnvEntry-Local";
	static final String rtag = "EnvEntry-Remote";
	
	private final IEnvEntry.Stub mbinder = new IEnvEntry.Stub() {

		public String LRead(String info) throws RemoteException {
			if (ltag != null) Log.v(ltag,"LRead:"+info);
			return LocalTupleSpace.getInstance(EnvEntry.this).read(info);
		}

		public List<String> LReadMany(String info) throws RemoteException {
			if (ltag != null) Log.v(ltag,"LReadMany:"+info);
			return LocalTupleSpace.getInstance(EnvEntry.this).readMany(info);
		}

		public String LTake(String info) throws RemoteException {
			if (ltag != null) Log.v(ltag,"LTake:"+info);
			return LocalTupleSpace.getInstance(EnvEntry.this).take(info);
		}

		public List<String> LTakeMany(String info) throws RemoteException {
			if (ltag != null) Log.v(ltag,"LTakeMany:"+info);
			return LocalTupleSpace.getInstance(EnvEntry.this).takeMany(info);
		}

		public void LWrite(String info, long startTime, long endTime)
				throws RemoteException {
			if (ltag != null) Log.v(ltag,"LWrite:"+info);
			EnvInfo e = new EnvInfo(info, startTime, endTime);
			LocalTupleSpace.getInstance(EnvEntry.this).write(e);
			
		}

		public void LWriteMany(List<String> info, long startTime, long endTime)
				throws RemoteException {
			if (ltag != null) Log.v(ltag,"LWriteMany:"+info);
			for(String i:info){
				LocalTupleSpace.getInstance(EnvEntry.this).write(new EnvInfo(i,startTime,endTime));
			}
		}

		public void notifyLWrite(String info, long startTime, long endTime)
				throws RemoteException {
			if (ltag != null) Log.v(ltag,"notifyLWrite:"+info);
			LocalTupleSpace.getInstance(EnvEntry.this).notifyWrite(new EnvInfo(info,startTime,endTime));
		}

		public void notifyLWriteMany(List<String> info, long startTime, long endTime)
				throws RemoteException {
			if (ltag != null) Log.v(ltag,"notifyLWriteMany:"+info);
			for(String i:info){
				LocalTupleSpace.getInstance(EnvEntry.this).notifyWrite(new EnvInfo(i,startTime,endTime));
			}						
		}


		@Deprecated
		public String RRead(String info) throws RemoteException {
			if (rtag != null) Log.w(rtag, "RRead");
			return null;
		}
		
		@Deprecated
		public List<String> RReadMany(String info) throws RemoteException {
			if (rtag != null) Log.w(rtag, "RReadMany");
			return null;
		}

		@Deprecated
		public String RTake(String info) throws RemoteException {
			if (rtag != null) Log.w(rtag, "RTake");
			return null;
		}

		@Deprecated
		public List<String> RTakeMany(String info) throws RemoteException {
			if (rtag != null) Log.w(rtag, "RTakeMany");
			return null;
		}

		public void RWrite(String info, long startTime, long endTime)
				throws RemoteException {
			if (rtag != null) Log.v(rtag,"RWrite:"+info + "["+startTime + "," + endTime + "]");
			EnvInfo e = new EnvInfo(info,startTime,endTime);
			RemoteTupleSpace.getInstance(EnvEntry.this).write(e);
			
		}

		public void RWriteMany(List<String> info, long startTime, long endTime)
				throws RemoteException {
			if (rtag != null) Log.v(rtag,"RWriteMany:"+info);
			for(String i:info){
				RemoteTupleSpace.getInstance(EnvEntry.this).write(new EnvInfo(i,startTime,endTime));
			}
		}
		
		public void notifyRWrite(String info, long startTime, long endTime)
				throws RemoteException {
			if (rtag != null) Log.v(rtag,"notifyRWrite:"+info);
//			Log.v("remoteTS","notifyRwrite -s");
			RemoteTupleSpace.getInstance(EnvEntry.this).notifyWrite(new EnvInfo(info,startTime,endTime));
//			Log.v("remoteTS","notifyRwrite -e");
		}
		
		public void notifyRWriteMany(List<String> info, long startTime, long endTime)
				throws RemoteException {
			if (rtag != null) Log.v(rtag,"notifyRWriteMany:"+info);
			for(String i:info){
				RemoteTupleSpace.getInstance(EnvEntry.this).notifyWrite(new EnvInfo(i,startTime,endTime));
			}
		}

		public String getIdentity() throws RemoteException {
			TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
			return tm.getSubscriberId();
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		
		return mbinder;
	}
}
